package com.atguigu.controller;

import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author chenxin
 * @date 2023/2/7
 * @Version 1.0
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{
    @DubboReference
    private RoleService roleService;

    @RequestMapping
    public String findRoles(Map map, HttpServletRequest request){
        //注意：点击角色管理和点击搜索，还有点击分页的超链接都是往这个controller方法发请求
        //1. 判断有没有查找条件(高级查询的条件、分页的条件)
        Map<String, Object> filters = getFilters(request);

//        List<Role> list = roleService.findAll();
        PageInfo<Role> page = roleService.findPage(filters);

        map.put("page",page);
        map.put("filters",filters);
        return "role/index";
    }

    @RequestMapping("/create")
    public String create(){
        return "role/create";
    }

    @RequestMapping("/save")
    public String save(Role role){
        roleService.insert(role);
        //新增完后，去到哪里？重定向到role的首页，效果不对，在弹出层中重定向
        /*
        * 1. 跳转到一个操作成功页面
        * 2. 操作成功页面有一个确定的按钮，点击确定关闭弹出层，刷新父级页面
        * */
        return "common/successPage";
    }

    @RequestMapping("/edit/{roleId}")
    public String edit(@PathVariable Long roleId,Map map){
        Role role = roleService.getById(roleId);
        map.put("role",role);
        return "role/edit";
    }

    @RequestMapping("/update")
    public String update(Role role){
        roleService.update(role);
        return "common/successPage";
    }

    @RequestMapping("/delete/{roleId}")
    public String delete(@PathVariable Long roleId){
        roleService.delete(roleId);
        //删除完去到哪里？
        return "redirect:/role";
    }
}
